package zhishuang.wang.sansi.fcms.protocol;

/**
 * @Author Zhishuang.Wang
 * @Date 2023/7/5 10:53
 * @Email 1406110602@qq.com
 */

import zhishuang.wang.sansi.tools.ReturnData;
import zhishuang.wang.sansi.tools.code.CodeConversion;
import zhishuang.wang.sansi.tools.time.DateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class FcmsProtocolDeal {
    public FcmsProtocolDeal() {
    }

    public static ReturnData fcmsDevVerionDeal(byte[] data, int dataLen) {
        int p_index = 0;

        ReturnData rd = new ReturnData();
        rd.setCode(1);
        if (dataLen > 6) {
            Map<String, String> map = new HashMap();
            dataLen -= 3;

            for(int i = 3; i < dataLen; ++i) {
                if ((data[i] & 255) == 0) {
                    p_index = i;
                    break;
                }
            }

            int versionLen;
            if (p_index == 0) {
                versionLen = dataLen - 3;
            } else {
                versionLen = p_index - 3;
            }

            String swVersion = ProtocolPackage.getAcsiiStr(data, 3, versionLen);
            map.put("version", swVersion);
            if (p_index > 3 && p_index < dataLen) {
                String majorVersion = String.format("%02X", data[p_index + 1]);
                String middleVersion = String.format("%02X", data[p_index + 2]);
                String minorVersion = String.format("%02X", data[p_index + 3]);
                map.put("majorVersion", majorVersion);
                map.put("middleVersion", middleVersion);
                map.put("minorVersion", minorVersion);
            }

            rd.setCode(0);
            rd.setData(map);
        } else {
            rd.setMessage("设备版本协议数据错误，请重试");
        }

        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData fcmsTimeReadDeal(byte[] data, int dataLen) {
        ReturnData rd = new ReturnData();
        rd.setCode(1);
        if (dataLen == 20) {
            String fcmsTime = ProtocolPackage.getAcsiiStr(data, 3, 14);
            String yearStr = fcmsTime.substring(0, 4);
            String monthStr = fcmsTime.substring(4, 6);
            String dayStr = fcmsTime.substring(6, 8);
            String hourStr = fcmsTime.substring(8, 10);
            String minStr = fcmsTime.substring(10, 12);
            String secStr = fcmsTime.substring(12, 14);
            String formatTimeStr = String.format("%s-%s-%s %s:%s:%s", yearStr, monthStr, dayStr, hourStr, minStr, secStr);
            Map<String, String> map = new HashMap();
            map.put("fcmsTime", formatTimeStr);
            rd.setCode(0);
            rd.setData(map);
        } else {
            rd.setMessage("设备时间读取协议数据错误，请重试");
        }

        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData fcmsParamReadDeal(byte[] data, int dataLen) {
        ReturnData rd = new ReturnData();
        rd.setCode(1);
        if (dataLen > 8) {
            int para = ProtocolPackage.getIntVal(data, 3, 2);
            Map<String, Integer> map = new HashMap();
            int devWidth;
            switch (para) {
                case 1:
                    if (dataLen == 12) {
                        devWidth = ProtocolPackage.getIntVal(data, 5, 4);
                        map.put("blockSize", devWidth);
                        rd.setCode(0);
                    }
                    break;
                case 2:
                    if (dataLen == 16) {
                        devWidth = ProtocolPackage.getIntVal(data, 5, 4);
                        int devHight = ProtocolPackage.getIntVal(data, 9, 4);
                        map.put("width", devWidth);
                        map.put("hight", devHight);
                        rd.setCode(0);
                    }
            }

            rd.setData(map);
        } else {
            rd.setMessage("设备参数读取协议数据错误，请重试");
        }

        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData fcmsLightAndModeReadDeal(byte[] data, int dataLen) {
        ReturnData rd = new ReturnData();
        rd.setCode(1);
        if (dataLen == 9) {
            int lightMode = data[3] & 255;
            byte[] lightValByte = Arrays.copyOfRange(data, 4, 6);
            String lightValStr = new String(lightValByte);
            int lightVal = Integer.valueOf(lightValStr);
            lightMode -= 48;
            Map<String, Integer> resMap = new HashMap();
            resMap.put("lightMode", lightMode);
            lightVal = ProtocolPackage.dimGradeToPer(lightVal);
            resMap.put("lightVal", lightVal);
            rd.setCode(0);
            rd.setData(resMap);
        } else {
            rd.setMessage("设备应答协议数据错误，请重试");
        }

        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData fcmsCommResDeal(byte[] data, int dataLen) {
        ReturnData rd = new ReturnData();
        rd.setCode(1);
        if (dataLen == 7) {
            Map<String, Integer> resMap = new HashMap();
            if (data[3] == 48) {
                resMap.put("setReturn", 0);
            } else {
                resMap.put("setReturn", 1);
            }

            rd.setCode(0);
            rd.setData(resMap);
        } else {
            rd.setMessage("设备应答协议数据错误，请重试");
        }

        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData fcmsShowInforDeal(byte[] data, int dataLen) {
        ReturnData rd = new ReturnData();
        rd.setCode(1);
        if (dataLen >= 21) {
            Map<String, String> resMap = new HashMap();
            byte[] indexNumByte = Arrays.copyOfRange(data, 3, 6);
            byte[] stopTimeByte = Arrays.copyOfRange(data, 6, 11);
            byte[] showModeByte = Arrays.copyOfRange(data, 11, 13);
            byte[] outSpeedByte = Arrays.copyOfRange(data, 13, 18);
            int showEndP = dataLen - 3;
            if (showEndP > 18) {
                byte[] showTextByte = Arrays.copyOfRange(data, 18, showEndP);
                String showContent = new String(showTextByte);
                if (showEndP > 19) {
                    int codeFlag = data[18] & 255;
                    codeFlag <<= 8;
                    codeFlag += data[19] & 255;
                    if (codeFlag != 65534 && codeFlag != 65279) {
                        showContent = CodeConversion.gbkToUtf8Code(showTextByte);
                    }
                }

                resMap.put("showContent", showContent);
            }

            resMap.put("indexNum", new String(indexNumByte));
            resMap.put("stopTime", new String(stopTimeByte));
            resMap.put("showMode", new String(showModeByte));
            resMap.put("outSpeed", new String(outSpeedByte));
            rd.setCode(0);
            rd.setData(resMap);
        } else {
            rd.setMessage("设备应答协议数据错误，请重试");
        }

        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData fcmsFalutDeal(byte[] data, int dataLen) {
        ReturnData rd = new ReturnData();
        rd.setCode(1);
        if (dataLen == 10) {
            byte[] falutInfor = Arrays.copyOfRange(data, 3, 7);
            String falutInforStr = new String(falutInfor);
            Integer falutInforInt = Integer.valueOf(falutInforStr, 16);
            Map<String, Integer> resMap = new HashMap();
            resMap.put("falutInfor", falutInforInt);
            rd.setCode(0);
            rd.setData(resMap);
        } else {
            rd.setMessage("设备应答协议数据错误，请重试");
        }

        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static byte[] fcmsValidData(byte[] data, int dataLen) {
        int i = 0;

        while(i < dataLen) {
            if (data[i] != 2) {
                ++i;
            } else {
                int j;
                for(j = i + 1; j < dataLen; ++j) {
                    if (data[j] == 3) {
                        return Arrays.copyOfRange(data, i, j + 1);
                    }
                }

                if (j >= dataLen) {
                    break;
                }
            }
        }

        return null;
    }
}
